/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.manchini.stereocv.telas;

import br.com.manchini.stereocv.utils.StereoCalib;
import com.sun.jna.Native;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

/**
 *
 * @author merito
 */
public class GerarParametrosRetificacao extends javax.swing.JDialog {

    /**
     * Creates new form GerarQ
     */
    public GerarParametrosRetificacao(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    public void gerarQ() throws IOException{
        File pasta = new File("img");
        File[] listFiles = pasta.listFiles();
        ArrayList<String> arquivos = new ArrayList<>();
        for (int i = 0; i < listFiles.length; i++) {
            arquivos.add(listFiles[i].getName());
        }
        StringBuilder conteudo = new StringBuilder();
        for (int i = 0; i < 999999; i++) {
            String direita = ""+i+"dir.jpg";
            String esquerda = ""+i+"esq.jpg";
            
            if(arquivos.contains(direita) && arquivos.contains(esquerda)){
                conteudo.append("img/").append(esquerda).append("\n").append("img/").append(direita).append("\n");
            }
            
        }
        
        File file = new File("list.txt");

        if (!file.exists()) {
            file.createNewFile();
        }
        FileOutputStream escritor = new FileOutputStream(file);
        OutputStreamWriter escritorBuf = new OutputStreamWriter(escritor);
        escritorBuf.append(conteudo);
        escritorBuf.close();
        escritor.close();
        
        StereoCalib stereoCalib = (StereoCalib) Native.loadLibrary("stereo_calibrate.so", StereoCalib.class);
//        StereoCalib stereoCalib = new StereoCalib();
        stereoCalib.gerarQ((Integer)jSpinner_tabx.getValue(), (Integer)jSpinner_taby.getValue(), (Float)jSpinner_tabTam.getValue());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jSpinner_tabx = new javax.swing.JSpinner();
        jLabel4 = new javax.swing.JLabel();
        jSpinner_taby = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();
        jSpinner_tabTam = new javax.swing.JSpinner();
        jPanel2 = new javax.swing.JPanel();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Tabuleiro:");
        jPanel1.add(jLabel1);

        jSpinner_tabx.setModel(new javax.swing.SpinnerNumberModel(9, 1, 12, 1));
        jPanel1.add(jSpinner_tabx);

        jLabel4.setText("x");
        jPanel1.add(jLabel4);

        jSpinner_taby.setModel(new javax.swing.SpinnerNumberModel(6, 0, 9, 1));
        jPanel1.add(jSpinner_taby);

        jLabel2.setText("Tamanho:");
        jPanel1.add(jLabel2);

        jSpinner_tabTam.setModel(new javax.swing.SpinnerNumberModel(Float.valueOf(2.5f), Float.valueOf(1.0f), Float.valueOf(25.0f), Float.valueOf(1.0f)));
        jPanel1.add(jSpinner_tabTam);

        getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);

        jButton2.setText("Gerar Parametros");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });
        jPanel2.add(jButton2);

        getContentPane().add(jPanel2, java.awt.BorderLayout.PAGE_END);

        setSize(new java.awt.Dimension(410, 136));
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        try {
            gerarQ();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GerarParametrosRetificacao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GerarParametrosRetificacao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GerarParametrosRetificacao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GerarParametrosRetificacao.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                GerarParametrosRetificacao dialog = new GerarParametrosRetificacao(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSpinner jSpinner_tabTam;
    private javax.swing.JSpinner jSpinner_tabx;
    private javax.swing.JSpinner jSpinner_taby;
    // End of variables declaration//GEN-END:variables
}